<?php

class User extends Controller {

    function User() {
        parent::Controller();
        $this->load->model("muser");
    }

    function index($cetak="") {
        $filter = isset($_POST['filter'])?$_POST['filter']:'';
        $data['result'] = $this->muser->search($filter);
        $data['filter'] = $filter;
        if($cetak=="print") {
            render("cetak_user",$data,"cetak_potrait");
        }else {
            render("index",$data);
        }
    }

    function add() {
        $valid = $this->_validasi_input();
        if($valid) {
            $this->muser->add($_POST['data']);
            $this->session->set_flashdata("notification","Data berhasil disimpan.");
            redirect("users/user/index");
        }else {
            render("input");
        }
    }

    function edit($id=0) {
        $data = $this->muser->get($id);
        $valid = $this->_validasi_input();
        if($valid) {
            $this->muser->edit($_POST['data']);
            $this->session->set_flashdata("notification", "Data berhasil diperbaharui");
            redirect("users/user/index");
        }elseif(empty($_POST)) {
            $_POST['data'] = $data;
            render("input");
        }else {
            render("input");
        }
    }

    function delete($id=0) {
        $data = $this->muser->get($id);
        if(!empty($data)) {
            $this->muser->delete($id);
            $this->session->set_flashdata("notification","Data berhasil dihapus.");
            redirect("users/user/index");
        }
    }

    function _validasi_input() {
        $rule_validation = array(
                array('field' => 'data[login]','label' => 'Username','rules' => 'required'),
                array('field' => 'data[password]','label' => 'Password','rules' => 'required')
        );
        $this->form_validation->set_rules($rule_validation);
        return $this->form_validation->run();
    }

    function retrieve_password() {
        $data = array();
        $rule_validation = array(
                array('field' => 'login','label' => 'Username','rules' => 'required'),
                array('field' => 'password','label' => 'Password','rules' => 'required'),
                array('field' => 'password2','label' => 'Konfirmasi Password','rules' => 'required|matches[password]')
        );
        $this->form_validation->set_rules($rule_validation);
        $valid = $this->form_validation->run();
        if($valid) {
            $user = $this->muser->get_by_login($_POST['login']);
            if(empty($user)) {
                $data["notification"] = "Maaf, pengguna tidak ditemukan";
            }else {
                $uid = $user['id'];
                $password = $_POST['password'];
                $this->muser->set_password($uid, $password);
                $data["notification"] = "Password berhasil dirubah.";
            }
        }
        render("retrieve_password",$data);

    }

    function ubah_password() {
        $rule_validation = array(
                array('field' => 'password','label' => 'Password','rules' => 'required'),
                array('field' => 'password2','label' => 'Konfirmasi Password','rules' => 'required|matches[password]')
        );
        $this->form_validation->set_rules($rule_validation);
        $valid = $this->form_validation->run();
        if($valid) {
            $uid = $this->session->userdata('authniter_user_id');
            $password = $_POST['password'];
            $this->muser->set_password($uid, $password);
            $this->session->set_flashdata("notification","Password berhasil dirubah.");
            redirect("home/index");
        }else {
            render("ubah_password");
        }
    }

    function login(){
        if(isset($_POST['username']) && isset($_POST['password'])){
            $this->authniter->login($_POST['username'],$_POST['password']);
        }else{
            $this->session->set_flashdata('notification','Maaf, Username dan Password Harus Diisi');
            redirect("");
        }
    }

    function logout(){
        $this->authniter->logout();
    }

}
